import { Component, OnInit, ViewChild, ViewContainerRef, ComponentRef, ComponentFactoryResolver, EventEmitter } from '@angular/core';
import { SingleService } from "./single.service";
import * as _ from 'lodash';
import { DataService } from 'app/services/data.service';
import { Util } from 'app/common/util';
import { AppPaginationComponent, PaginationModel } from "../../../../../widgets/pagination/pagination.component";
import { TableComponent } from "../../modal/table/table.component";

@Component({
  selector: 'app-single',
  templateUrl: './single.component.html',
  styleUrls: ['./single.component.scss'],
  providers: [SingleService]
})
export class SingleComponent implements OnInit {
  // 模版
  @ViewChild("singleTable", { read: ViewContainerRef })
  singleTable: ViewContainerRef;
  singleTableComponent: ComponentRef<TableComponent>;
  // 用于识别指标类型
  public type: any;
  // 上方查询体
  public getBody: any = {
    pageSize: 10,
    pageIndex: 1,
    beginDate: '',
    endDate: '',
    nIxId: ''
  }
  //分页配置
  public listPageInfo = {
    currentPageNum: 1,
    pageSize: 10,
    totalPages: 1,
    total: 0,
    pagesShow: 5,
    startRow: 0,
    endRow: 0,
    pageList: [5, 10, 25, 50, 100]
  }
  // 列表新数据
  public dataList: any = [];
  // 克隆列表新数据,用于页面数据标红对比
  public isChangeList: any = [];
  // 用于是否显示下拉框
  public isEditList: any = [];

  constructor(
    public resolver: ComponentFactoryResolver,
    public singleService: SingleService
  ) { }

  ngOnInit() {
    // 加载table
    this.showSingleTable();
    this.list();
  }
  // 下方多级列表
  list(pageNum?, pageSize?) {
    let that = this;
    this.getBody.pageIndex = pageNum ? pageNum : this.listPageInfo.currentPageNum;
    this.getBody.pageSize = pageSize ? pageSize : this.listPageInfo.pageSize;
    new Promise((resolve, reject) => {
      this.singleService.list(this.getBody).subscribe(data => {
        if (data) {
          that.dataList = data.list;
          // 用于标红对比
          that.isChangeList = _.cloneDeep(data.list);
          // 用于是否显示下拉框
          that.isEditList = _.cloneDeep(data.list);
          that.listPageInfo.currentPageNum = data.pageNum;
          that.listPageInfo.totalPages = data.pages;
          that.listPageInfo.total = data.total;
          that.listPageInfo.startRow = data.startRow;
          that.listPageInfo.endRow = data.endRow;
          _.forEach(that.dataList, (item, index: any) => {
            if (index > 0) {
              // 前后条数据对比
              _.forIn(item, (value, key) => {
                if (that.dataList[index - 1][key] == that.dataList[index][key]) {
                  that.isChangeList[index][key] = false;
                  that.isEditList[index][key] = 'no';
                } else {
                  that.isChangeList[index][key] = true;
                  that.isEditList[index][key] = 'no';
                }
              })
            } else {
              // 第一条数据不作对比 
              _.forIn(item, (value, key) => {
                that.isChangeList[index][key] = false;
                that.isEditList[index][key] = 'no';
              })
            }
          })
          resolve(data);
        }
      })
    }).then(data => {
      that.singleTableComponent.instance.isCheckedAll = false;
      that.singleTableComponent.instance.dataList = _.cloneDeep(that.dataList);
      that.singleTableComponent.instance.isChangeList = _.cloneDeep(that.isChangeList);
      that.singleTableComponent.instance.isEditList = _.cloneDeep(that.isEditList);
    })
  }
  // 分页
  sizeChange(pageSize: number) {
    if (pageSize !== this.listPageInfo.pageSize) {
      this.listPageInfo.pageSize = pageSize;
      this.list();
    }
  }
  // 分页
  pageNavigation(currentPageNum: number) {
    this.listPageInfo.currentPageNum = currentPageNum;
    this.list(this.listPageInfo.currentPageNum, this.listPageInfo.pageSize);

  }
  // 关闭流程状态模态框
  closeFolw() {
    window['$']('#single').modal('hide');
    window['$']('#singles').modal('hide');
    this.singleTableComponent.destroy();
  }
  // 模版
  showSingleTable() {
    if (this.singleTableComponent && this.singleTableComponent.instance) {
      this.singleTableComponent.destroy();
    }
    const singleTableComponent = this.resolver.resolveComponentFactory(TableComponent);
    this.singleTableComponent = this.singleTable.createComponent(singleTableComponent);
    this.singleTableComponent.instance.isShowCol1 = 'sFundCode';
    this.singleTableComponent.instance.isShowCol2 = 'sFullName';
    this.singleTableComponent.instance.isShowCol3 = 'sOprDate';
  }
}
